//
//  M4PagoViewController.swift
//  M4
//
//  Created by YuChuang on 2024/8/6.
//

import UIKit

class M4PagoViewController: M4BaseViewController {
    
    private var M4PagoListModel: M4LoanResultModel?
    
    lazy var M4NullView: M4DataNullView = {
        let nullV = UINib(nibName: "M4DataNullView", bundle: nil).instantiate(withOwner: self).last as! M4DataNullView
        nullV.M4NullUpdateUI(describe: "No hay préstamos pendientes de pago", buttonTitle: "Pedir un préstamo")
        nullV.M4NullButtonBlock = {
            self.tabBarController?.selectedIndex = 0
        }
        return nullV
    }()
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        M4PagoListNet()
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        M4NavTitle(M4Title: "Historial de préstamos")
        M4NeedLeft = false
        view.addSubview(M4TableView)
        M4TableView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalToSuperview().offset(M4NavigationFullHeight())
            make.bottom.equalToSuperview().offset(-M4TabBarFullHeight())
        }
        M4addRefreshToTaleView { [weak self] in
            self?.M4PagoListNet()
        }
    }
    
    override func M4DataRefresh() {
        M4PagoListNet()
    }
    
    override func M4CellAction(_ action: M4IDAction) { //M4PagoDetailViewController
        let type = M4HistoryRepayAction(rawValue: action.M4ActionType)
        if type == .M4HisPagaAction {
            let data = action.M4ActionData as? Int
            let page = M4PagoDetailViewController()
            page.M4PagoId = data ?? 0
            self.navigationController?.pushViewController(page, animated: true)
        }
        else if type == .M4ProrrogaAction {
            let data = action.M4ActionData as? M4LoanOrderListModel
            let page = M4ProrrogaDetiailViewController()
            page.M4ProrrogaData = data?.XPvy?[0]
            page.M4MainLoanId = data?.Jajlc ?? 0
            page.M4ProductItemCode = data?.NbN ?? ""
            page.productName = data?.LcMnJMQqO ?? ""

            self.navigationController?.pushViewController(page, animated: true)
        }
    }
    
    private func M4PagoDataSource() {
        M4TableSources.removeAll()
        M4PagoListModel?.QKDLhYTHobF?.forEach({ order in
            M4TableSources.append(M4TableViewCellData("M4HisRepayCell", XIB: true, order))
        })
        M4TableView.reloadData()
        M4TableView.backgroundView = M4PagoListModel?.QKDLhYTHobF?.count ?? 0 <= 0 ? M4NullView : nil
    }
    
    
    private func M4PagoListNet() {
        let para: [String : Any] = ["YjWGJmF": "ESP", "DnVc": M4UserDataManager.m4UserShare.M4Itemcode, "IxENPg": M4UserDataManager.m4UserShare.M4Token, "Uvmyi": 2]
        M4NetworkManager().M4Request(M4NetworkTarget.M4_targetType(.m4OrderDetail, .post, params: para)) { model in
            self.M4endRefresh()
            if model.code == 1 {
                let data = model.data as? [String : Any]
                self.M4PagoListModel = data?.kj.model(M4LoanResultModel.self)
            }
            else {
                self.view.makeToast(model.message, position: .center)
            }
            self.M4PagoDataSource()
        }
    }

}
